package top.chukongxiang.monitorservice.tools;

import cn.hutool.core.util.StrUtil;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;
import javafx.stage.Modality;
import javafx.stage.Window;
import top.chukongxiang.monitorservice.javafx.fxml.components.LoadingAlert;
import top.chukongxiang.monitorservice.model.exception.FxException;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Optional;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/26 17:29
 */
public class AlertUtil {

    public static void error(Window parent, String title, String header, String content) {
        alert(parent, Alert.AlertType.ERROR, title, header, content);
    }

    public static void error(Window parent, String header, String content) {
        error(parent, "错误", header, content);
    }

    public static void error(Window parent, String header) {
        error(parent, "错误", header, null);
    }

    public static void error(Window parent, Exception e) {
        FxException fxException = FxException.getFxException(e);
        String header = fxException.getMessage();
        error(parent, header, e);
    }

    public static void error(Window parent, String header, Exception e) {
        String stackTree = "";
        try (StringWriter stringWriter = new StringWriter()) {
            try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
                e.printStackTrace(printWriter);
                stackTree = stringWriter.toString();
            }
        } catch (IOException ignore) {
        }
        error(parent, header, stackTree);
    }


    public static void info(Window parent, String title, String header, String content) {
        alert(parent, Alert.AlertType.INFORMATION, title, header, content);
    }

    public static void info(Window parent, String header, String content) {
        info(parent, "提示", header, content);
    }

    public static void info(Window parent, String header) {
        info(parent, "提示", header, null);
    }

    public static void warn(Window parent, String title, String header, String content) {
        alert(parent, Alert.AlertType.WARNING, title, header, content);
    }

    public static void warn(Window parent, String header, String content) {
        warn(parent, "警告", header, content);
    }

    public static void warn(Window parent, String header) {
        warn(parent, "提示", header, null);
    }

    public static void alert(Window parent, Alert.AlertType alertType, String title, String header, String content) {
        Alert alert;
        switch (alertType) {
            case INFORMATION -> {
                alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
            }
            case WARNING -> {
                alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("警告");
            }
            case ERROR -> {
                alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
            }
            case NONE -> {
                alert = new Alert(Alert.AlertType.NONE);
                alert.setTitle("");
            }
            case CONFIRMATION -> {
                alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("确认");
            }
            default -> {
                throw new FxException("不支持的弹框类型");
            }
        }
        if (parent != null) {
            alert.initOwner(parent);
            alert.initModality(Modality.WINDOW_MODAL);
        } else {
            alert.initModality(Modality.APPLICATION_MODAL);
        }
        alert.setTitle(title);
        alert.setHeaderText(header);
        DialogPane dialogPane = alert.getDialogPane();
        if (StrUtil.isBlank(content)) {
            dialogPane.setExpanded(false);
        } else {
            TextArea expandableContent = (TextArea) dialogPane.getExpandableContent();
            if (expandableContent == null) {
                expandableContent = new TextArea();
                expandableContent.setEditable(false);
                expandableContent.setWrapText(true);
                dialogPane.setExpandableContent(expandableContent);
            }
            expandableContent.setText(content);
        }
        alert.show();
    }


    public static Optional<ButtonType> confirm(Window parent, String title, String header, String content) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        if (parent != null) {
            alert.initOwner(parent);
            alert.initModality(Modality.WINDOW_MODAL);
        } else {
            alert.initModality(Modality.APPLICATION_MODAL);
        }
        alert.setTitle(title);
        alert.setHeaderText(header);
        DialogPane dialogPane = alert.getDialogPane();
        if (StrUtil.isBlank(content)) {
            dialogPane.setExpanded(false);
        } else {
            TextArea expandableContent = (TextArea) dialogPane.getExpandableContent();
            if (expandableContent == null) {
                expandableContent = new TextArea();
                expandableContent.setEditable(false);
                expandableContent.setWrapText(true);
                dialogPane.setExpandableContent(expandableContent);
            }
            expandableContent.setText(content);
        }
       return alert.showAndWait();
    }

    public static LoadingAlert loading() {
        return loading(null);
    }
    public static LoadingAlert loading(Window parent) {
        return loading(parent, "加载中...");
    }
    public static LoadingAlert loading(Window parent, String text) {
        return new LoadingAlert(parent, text);
    }

}
